package com.xcms.member;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class OnlineServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = request.getServletContext();
        List<HttpSession> clients =(ArrayList<HttpSession>) context.getAttribute("clients");
        List<Online> onlines=new ArrayList<>();
        if(clients!=null&&clients.size()>0)
       {
           for (HttpSession session:clients) {
                Online online=new Online();
                //todo自己下去转换时间
                online.setCreattime(session.getCreationTime()+"");
                online.setLasttime(session.getLastAccessedTime()+"");
                online.setSessionid(session.getId());
                online.setUsername(session.getAttribute("username")+"");
                onlines.add(online);
           }
           request.setAttribute("onlines",onlines);
           request.getRequestDispatcher("admin/online.jsp").forward(request,response);
       }
    }
}
